Skip to content

[AIRFLOW-1560] Add AWS DynamoDB hook and operator for inserting batch items#2560

Closed
sid88in wants to merge 6 commits into
apache:masterfrom
sid88in:feature/dynamodb_hooks
Closed

[AIRFLOW-1560] Add AWS DynamoDB hook and operator for inserting batch items#2560
sid88in wants to merge 6 commits into
apache:masterfrom
sid88in:feature/dynamodb_hooks

Conversation

@sid88in

@sid88in sid88in commented Sep 3, 2017

Copy link
Copy Markdown
Contributor

Dear Airflow maintainers,

Please accept this PR. I understand that it will not be reviewed until I have checked off all the steps below!

JIRA

Description

  • Here are some details about my PR, (No UI Changes):

The PR addresses airflow integration with AWS Dynamodb.

Currently there is no hook to interact with DynamoDb for reading or writing items (single or batch insertions). To get started, we want to push data in DynamoDB using airflow jobs (scheduled daily). Idea is to read aggregates from Hive and push in DynamoDB (write data job will run everyday to make this happen). First we want to create DynamoDB hooks (this PR addressed the same) and then create operator to move data from Hive to DynamoDB.
http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#service-resource

Something to think about - Performance
I think airflow's generic approach for reading all data from source and then transferring to the destination by either storing source data in memory or disk is not the best optimized approach (in cases of big data with potential performance issues). When I started my initial design - I categorized it into 2 use cases a) small data b) big data.. The operators written in airflow code right now mostly handle small data (so does this PR). My approach would be to start with this operator (small data) and then handle big data use case if we see performance issues. I have some thoughts on how we can handle big data operations (using batching mechanism). For instance:

  1. Fetch all distinct id's (primary key) from source
  2. Create N buckets and assign bucket number to all the id's
  3. For each bucket : fetch data from source and then insert into destination.

Plan to handle this in subsequent PR.

Something to think about - Other ways of transferring data from hive to dynamodb

There can be multiple ways for this data transfer to occur and here are some of the thoughts on current design decisions (KISS - keep it simple and sweet):

  1. Lambda function : You can write a lambda function to transfer data and trigger the function using AwsLambdaHook ([AIRFLOW-1571] Add AWS Lambda Hook #2570) but lambda lifetime is 5 mins and if the data transfer takes longer lambda will die.

  2. Amazon Ecosystem (Amazon EMR, Big Data Pipeline, AWS Glue): Not to forget adding these systems create additional complexities (aws dependencies, aws limits, system complexities, IAM issues, airflow integrations with additonal dependencies and ofcourse $$)

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBPipeline.html

  1. Not to forget insertions in dynamodb are tricky because of WCU's and boto handles the insertions gracefully by automatically throttling insertions (buffering etc)

Tests

  • My PR adds the following unit tests :
  1. test_aws_hook.py
    adds mock_dynamodb2 from moto (https://github.com/spulec/moto). Wrote a test case to get aws resource object for dynamodb and create table via resource object and assert if count of items in table is 0 (implies table was created).

  2. test_dynamodb_hook.py
    adds mock_dynamodb2 from moto (https://github.com/spulec/moto). Wrote a test case to get aws resource object for dynamodb and create/insert data in dynamodb table. Then this unit test writes 10 items in the table using write_batch_data operation

  3. HiveToDynamoDBTransferTest
    this unit test checks the workings of HiveToDynamoDB operator by inserting 1 record in dynamodb table via write operation.

Commits

  • My commits all reference JIRA issues in their subject lines, and I have squashed multiple commits if they address the same issue. In addition, my commits follow the guidelines from "How to write a good git commit message":
    1. Subject is separated from body by a blank line
    2. Subject is limited to 50 characters
    3. Subject does not end with a period
    4. Subject uses the imperative mood ("add", not "adding")
    5. Body wraps at 72 characters
    6. Body explains "what" and "why", not "how"

@mention-bot

Copy link
Copy Markdown

@sid88in, thanks for your PR! By analyzing the history of the files in this pull request, we identified @rfroetscher, @poulainv and @jlowin to be potential reviewers.

@sid88in sid88in changed the title Add AWS DynamoDB hooks to write batch data Add AWS DynamoDB hook for inserting batch items Sep 3, 2017
@sid88in sid88in changed the title Add AWS DynamoDB hook for inserting batch items [AIRFLOW-1560] Add AWS DynamoDB hook for inserting batch items Sep 3, 2017
@codecov-io

codecov-io commented Sep 4, 2017

Copy link
Copy Markdown

Codecov Report

Merging #2560 into master will increase coverage by 0.06%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2560      +/-   ##
==========================================
+ Coverage    70.9%   70.96%   +0.06%     
==========================================
  Files         150      150              
  Lines       11595    11617      +22     
==========================================
+ Hits         8221     8244      +23     
+ Misses       3374     3373       -1
Impacted Files Coverage Δ
airflow/utils/log/file_task_handler.py 72.28% <0%> (-2.41%) ⬇️
airflow/bin/cli.py 51.74% <0%> (ø) ⬆️
airflow/task_runner/base_task_runner.py 73.33% <0%> (ø) ⬆️
airflow/models.py 87.26% <0%> (+0.13%) ⬆️
airflow/jobs.py 78.11% <0%> (+0.16%) ⬆️
airflow/hooks/hive_hooks.py 40.17% <0%> (+0.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af91e2a...c826b2b. Read the comment docs.

@sid88in

sid88in commented Sep 4, 2017

Copy link
Copy Markdown
Contributor Author

This is my first PR for airflow. Still getting my head around the build errors :)

@sid88in sid88in changed the title [AIRFLOW-1560] Add AWS DynamoDB hook for inserting batch items [AIRFLOW-1560] Add AWS DynamoDB hook and operator for inserting batch items Sep 4, 2017
@sid88in
sid88in force-pushed the feature/dynamodb_hooks branch from d07c1d4 to 3a50662 Compare September 5, 2017 21:55
@sid88in sid88in closed this Sep 6, 2017
@sid88in sid88in reopened this Sep 6, 2017
@sid88in
sid88in force-pushed the feature/dynamodb_hooks branch from 7067536 to 3552901 Compare September 6, 2017 19:45
@sid88in sid88in closed this Sep 6, 2017
@sid88in sid88in reopened this Sep 6, 2017
@sid88in sid88in closed this Sep 8, 2017
@sid88in
sid88in deleted the feature/dynamodb_hooks branch September 8, 2017 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants